package com.shopping.mapper;

import com.shopping.pojo.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartMapper {

    //添加商品至购物车
    @Select("SELECT COUNT(*) FROM order_item WHERE user_id = #{userId} AND product_id = #{productId} AND status = #{status}")
    int checkItemExists(@Param("userId") int userId, @Param("productId") int productId, @Param("status") int status);

    @Update("UPDATE order_item SET quantity = quantity + #{quantity} WHERE user_id = #{userId} AND product_id = #{productId} AND status = #{status}")
    void updateCartItemQuantity(@Param("userId") int userId, @Param("productId") int productId, @Param("quantity") int quantity, @Param("status") int status);

    @Insert("INSERT INTO order_item (user_id, product_id, image, name, quantity, price, status) " +
            "VALUES (#{userId}, #{productId}, #{image}, #{name}, #{quantity}, #{price}, #{status})")
    void insertCartItem(Cart cart);

    //查询购物车商品数据
    @Select("select * from order_item where status = 0 AND user_id=#{customerId}")
    List<Cart> getCartList(Integer customerId);

    //修改单条商品数据
    void updateSingleCartItem( Cart cart);
    //购买后减少商品库存
    @Update("UPDATE product Set stock=stock-#{quantity} WHERE id = #{productId} AND stock >= #{quantity}")
    void deleteProductStock( Cart cart);

    @Delete("delete from order_item where id=#{id}")
    void deleteCart(Integer id);

    //查询订单列表
    @Select("select * from order_item where status = 1 AND user_id=#{customerId} ORDER BY purchase_time DESC")
    List<Cart> getOrderList(Integer customerId);

    //查询全部订单
    @Select("select * from order_item where status = 1 ORDER BY purchase_time DESC")
    List<Cart> getAllOrder();
}
